<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>数组算法</title>
</head>

<body>
	<script type="text/javascript">
		// 数组排序 sort方法	
		const arr = [123, 203, 23, 13, 34, 65, 65, 45, 89, 13, 1];
		const arrSort1 = [...arr].sort((a, b) => a - b)  //顺序
		const arrSort2 = [...arr].sort((a, b) => b - a)  //逆序
		console.log(arrSort1, "顺序");
		console.log(arrSort2, "逆序");

		// 选择排序
		const arr1 = [123, 203, 23, 13, 34, 65, 65, 45, 89, 13, 1];
		for (var i = 0; i < arr1.length - 1; i++) {
			//每一轮要比较多少次
			for (var j = 0; j < arr1.length - 1 - i; j++) {
				//如果第一个比第二个大，则交换位置
				if (arr1[j] > arr1[j + 1]) {
					let temp = arr1[j]
					arr1[j] = arr1[j + 1]
					arr1[j + 1] = temp
				}
			}
		}
		console.log(arr1, "选择排序");

		//冒泡排序
		const arr2 = [123, 203, 23, 13, 34, 65, 65, 45, 89, 13, 1];
		for (let i = 0; i < arr2.length - 1; i++) {
			//每一轮要比较多少次
			for (let j = 0; j < arr2.length - 1; j++) {
				//如果第一个比第二个大，则交换位置
				if (arr2[j] > arr2[j + 1]) {
					let temp = arr2[j]
					arr2[j] = arr2[j + 1]
					arr2[j + 1] = temp
				}
			}
		}
		console.log(arr2, "冒泡排序");

	</script>
</body>

</html>